package com.siashan.unipay4j.demo.web;

import com.alibaba.fastjson.JSON;
import com.siashan.unipay4j.core.Result;
import com.siashan.unipay4j.core.pay.ICancel;
import com.siashan.unipay4j.core.pay.IClose;
import com.siashan.unipay4j.core.pay.IPayment;
import com.siashan.unipay4j.core.pay.PayModel;
import com.siashan.unipay4j.demo.common.AjaxResult;
import com.siashan.unipay4j.wx.WxPayFactoryProvider;
import com.siashan.unipay4j.wx.pay.WxTradeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 *
 * @author siashan
 * @since v1.0.1
 **/
@RestController
@RequestMapping("wxpay")
public class WxPayController {

    @Autowired
    private WxPayFactoryProvider wxPayFactoryProvider;

    @GetMapping("test")
    public AjaxResult test(){
        return AjaxResult.success("hahaha");
    }

    /**
     * 公众号支付
     *
     * @return
     */
    @GetMapping("jsapi")
    public AjaxResult jsapi(){
        PayModel payModel = new PayModel();
        payModel.setOutTradeNo("2022091300001");
        payModel.setBody("测试微信V3支付");
        payModel.setNotifyUrl("https://1511w8t629.51mypc.cn/wepay/test");
        payModel.setPrice(new BigDecimal("0.01"));
        payModel.setOpenid("os_Eg473P6oF80Q2T0r8KOkeuwR8");

        IPayment payment = wxPayFactoryProvider.getPayment(WxTradeTypeEnum.XCX.name());
        Result<Object> pay = payment.pay(payModel);
        System.out.println(JSON.toJSONString(pay));
        return AjaxResult.success("hahaha");
    }


    /**
     * 关闭订单
     *
     * @return
     */
    @GetMapping("close")
    public AjaxResult close(){
        String outTradeNo = "2022091300001";
        IClose close = wxPayFactoryProvider.getClose();
        Result<Object> pay = close.execClose(outTradeNo);
        System.out.println(JSON.toJSONString(pay));
        return AjaxResult.success("hahaha");
    }
}
